﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;

namespace PeisongWeb.admin
{
    public partial class categoryEdit : adminPage
    {


        protected int id
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["id"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["id"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("BB", 1))
                {
                    if (Request.QueryString["category"] != null)
                    {
                        if (Library.Number.IsIntPositive(Request.QueryString["category"]))
                        {
                            id = Convert.ToInt32(Request.QueryString["category"]);
                        }
                    }
                    drpBind();
                    if (id != 0)
                    {

                        getDetails();
                    }
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
                
                
                
            }
        }
        private void getDetails()
        {
            CategoryEntity category = logic.category.getById(id);
            txt_Name.Text = category.Name;

            txt_Treelevel.Text = category.Treelevel.ToString();
            txt_Sort.Text = category.Sort.ToString();
            txt_Ab.Text = category.Shortname;
            drp_ParentCategory.SelectedIndex = drp_ParentCategory.Items.IndexOf(drp_ParentCategory.Items.FindByValue(category.ParentId.ToString()));
            chx_isShow.Checked = category.IsShow;
        }
        private void drpBind()
        {
            drp_ParentCategory.DataSource = logic.category.select("treelevel=0","categoryId",1000,1).Tables[1];
            drp_ParentCategory.DataBind();
            drp_ParentCategory.Items.Insert(0, new ListItem("--无--", "0"));
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (logic.category.isExistShortname(id ,txt_Ab.Text.Trim()))
            {
                Library.Script.ClientMsg("已经存在类别缩写，请更换！");
                return;
            }

            CategoryEntity cat = new CategoryEntity();

            cat.Name = txt_Name.Text;
            cat.Shortname = txt_Ab.Text;

            cat.Sort = txt_Sort.Text != "" ? Convert.ToInt32(txt_Sort.Text) : 1;

            cat.Treelevel = txt_Treelevel.Text != "" ? Convert.ToInt32(txt_Treelevel.Text) : 0;
            cat.ParentId = Convert.ToInt32(drp_ParentCategory.SelectedItem.Value);
            cat.IsShow = chx_isShow.Checked;
            if (id != 0)
            {
                cat.CategoryId = id;
                logic.category.update(cat);
                Library.Script.ClientMsgUrl("保存成功！", "categoryList.aspx");
            }
            else
            {
                logic.category.insert(cat);
                if (cat.ParentId!=0 && cat.IsShow==true)
                {
                    Library.Script.ClientMsgUrl("添加成功,请初始化系统加价体系！", "systemPriceModulus.aspx");
                }
                else
                {
                    Library.Script.ClientMsgUrl("保存成功！", "categoryList.aspx");
                }
            }
        }
    }
}
